var index;

var table;
var form;

$(document).ready(function () {
    $('#page-toolbar-content').show();

    layui.use(['table', 'form'], function () {

        table = layui.table;
        form = layui.form;
        form.render();

        getAllButton(currUrl).done(function(returnData) {
            createButton(returnData);
            initButtonEvent();
            initTable();
        });

    });

});

function initButtonEvent() {

    var $ = layui.$, active = {

        add: function () {
            loadingChildPage('/admin/category/category_add', '新增收费类别');
        },
        edit: function () {
            var checkStatus = table.checkStatus('category-table-content');
            var data = checkStatus.data;
            if (data.length <= 0) {
                layer.msg("没有选取任何行！");
            }
            else if (data.length > 1) {
                layer.msg("无法批量修改行！");
            }
            else {
                loadingChildPageId('/admin/category/category_edit', '编辑收费类别', data[0].categoryId);
            }
        },
        del: function () {
            var checkStatus = table.checkStatus('category-table-content');
            var data = checkStatus.data;
            if (data.length <= 0) {
                layer.msg("没有选取任何行！");
            }
            else {
                var dataIds = [];
                for (var i = 0; i < data.length; i++) {
                    dataIds[i] = data[i].categoryId;
                }

                removeCategory(dataIds);
            }
        },
        search: function () {

            var categoryName = $('#categoryName');

            // 执行重载
            table.reload('category-table-content', {
                page: {
                    curr: 1
                }
                , where: {
                    categoryName: categoryName.val()
                }
            });

            $('.layui-table-page').css('text-align', 'right');
            $('.layui-table-page').css('padding-right', '20px');

        }
    };

    $('.menu-btn-group .layui-btn, .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

}

function initTable() {

    table.render({
        elem: '#category-table-content'
        , url: apiHostUrl + '/admin/category/queryCategory'
        , headers: {Authorization: token}
        , response: {
            statusName: 'errorCode'
            , statusCode: 0
            , msgName: 'errorMsg'
            , countName: 'totalCount'
            , dataName: 'returnData'
        }
        , cellMinWidth: 50
        , limit: 15
        , limits: [15]
        , cols: [[
            {type: 'checkbox'}
            , {field: 'categoryId', width: 100, title: 'ID'}
            , {field: 'categoryName', width: 300, title: '收费类别名称'}
            , {field: 'categoryUnit', width: 150, title: '收费类别单位'}
            , {field: 'categoryPrice', width: 150, title: '收费类别单价'}
            , {field: 'categoryDesc', title: '收费类别说明'}
            , {field: 'toolbar', width: 180, title: '操作', align: 'center', toolbar: '#tableToolbar'}
        ]]
        , page: {
            layout: ['prev', 'page', 'next', 'skip', 'limit', 'count']
        }
        , id: 'category-table-content'
    });

    // 监听工具条
    table.on('tool(page-table)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            var dataIds = [];
            dataIds[0] = data.categoryId;
            removeCategory(dataIds);
        } else if (obj.event === 'edit') {
            loadingChildPageId('/admin/category/category_edit', '编辑收费类别', data.categoryId);
        }
    });

    $('.layui-table-page').css('text-align', 'right');
    $('.layui-table-page').css('padding-right', '20px');

}

function removeCategory(categoryIds) {
    layer.confirm("真的要删除该行吗？", function () {
        ajax(
            "/admin/category/delCategory", "POST",
            {categoryIds: categoryIds}
            , true, true, true
        ).done(function () {
            loadingPage("/admin/category/category");
        });
        layer.closeAll();
    });
}